# !/user/bin/env python3
# -*- coding: utf-8 -*-
""""
    页面对象层编写技巧：
        类名：使用大驼峰将模块名称抄进来，有下划线去掉下划线
        方法：根据业务需求每个步骤单独封装一个方法
        方法名：page_xx
"""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By


# 页面对象层
class PageLogin:

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(2)
        self.driver.get("https://www.baidu.com/")

    # 点击百度登录按钮
    def page_click_setup_button(self):
        self.driver.find_element(By.CSS_SELECTOR, ".s-top-login-btn").click()

    # 点击短信登录
    def page_sms_login_link(self):
        self.driver.find_element(By.CSS_SELECTOR, "#TANGRAM__PSP_11__changeSmsCodeItem").click()

    # 输入手机号
    def page_input_phone_number(self, phone_number):
        self.driver.find_element(By.CSS_SELECTOR, "#TANGRAM__PSP_11__smsPhone").send_keys(phone_number)

    # 输入验证码
    def page_input_captcha(self, captcha):
        self.driver.find_element(By.CSS_SELECTOR, "#TANGRAM__PSP_11__smsVerifyCode").send_keys(captcha)

    # 点击登录
    def page_click_login_button(self):
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#TANGRAM__PSP_11__smsSubmit").click()

    # 获取错误信息
    def page_get_error_info(self):
        time.sleep(1)
        return self.driver.find_element(By.CSS_SELECTOR, ".pass-generalError").text

    # 组装登录业务方法，给业务层调用
    def page_login(self, phone_number, captcha):
        self.page_click_setup_button()
        self.page_sms_login_link()
        self.page_input_phone_number(phone_number)
        self.page_input_captcha(captcha)
        self.page_click_login_button()

